<?php
// +----------------------------------------------------------------------
// | 日志管理
// +----------------------------------------------------------------------
// | @link ( http://www.yurnero.net )
// +----------------------------------------------------------------------
// | @copyright
// +----------------------------------------------------------------------
// | @licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author Haijun Wu <nicholasinlove@126.com>
// +----------------------------------------------------------------------
// | $Id: PageAction.class.php 122 2011-05-18 09:00:37Z nicholasinlove1986@gmail.com $
// +----------------------------------------------------------------------


class PageAction extends BaseAction {

	public function index() {
		redirect(__APP__);
	}
	
	//发表日志
	public function addPage() {
		$user =  D('AdminUser');
		$nick_name   =  $user->getUserNickName(true);
		
		$this->assign('action_link', array('href'=>__URL__.'/listPage/', 'text' => L('listPage')));
		$this->assign('ur_here',L('addPage'));
		$this->assign('nick_name', $nick_name);
		$this->display();	
	}

	//发表日志
	public function insertPage() {
		if (isset($_REQUEST['act'])) {	
			$page = D('Posts');
			$data = $page->create();
			if (false === $data) {
				$this->error($page->getError());
			} else {
				if ($result = $page->add($data)) {
					clear_cache_files();    // 清除缓存
					set_log('add(Page, Id: '.$result.')');
					$this->assign('jumpUrl',__URL__.'/listPage/');
					$this->success(L('success'));
				} else {
					$this->error(L('error'));
				}
			}
		} else {
			$this->error (L('illegal'));
		}	
	}
	
	//日志列表
	public function listPage() {
		//排序字段 默认为主键
		if (isset($_REQUEST ['order'])) {
			$order = $_REQUEST ['order'];
		} else {
			$order = 'posts_id';
		}
		//排序方式 默认按照倒序排列
		if (isset($_REQUEST ['sort'])) {
			$sort = $_REQUEST ['sort'];
		} else {
			$sort = 'desc';
		}
		//查询
		$map  =	 array();		
		$map['posts_status']  =	 1;
		$map['posts_type']    =	 2;

		if ($_REQUEST['posts_author']) {
			$map['posts_author'] = $_REQUEST['posts_author'];
		}	
		if ($_REQUEST['keyword']!='') {
			$map['posts_title'] = array('like',"%{$_REQUEST['keyword']}%");
		}
		if (!empty($_REQUEST['key'])) {
			$map = json_decode(base64_url_decode($_REQUEST['key']));
		} 		
		//列表
		$page   =  M('Posts');
		$user   =  D('AdminUser');

		$nick_name   =  $user->getUserNickName(true);
		
		import("@.ORG.Page");
		C ( 'PAGE_ROLLPAGE',C('page_roll') );	
		$count = $page->where($map)->count();
		$p = new Page ($count, C('admin_list_num'));	
		$list = $page->where($map)->field('posts_id,posts_author,posts_title,posts_url,posts_tracknum,posts_comment,
		posts_draft,posts_istrack,posts_hit,posts_allowcom,posts_addtime')->order("`".$order."` ".$sort)->limit($p->firstRow.','.$p->listRows)->findAll();
		$p->setConfig('theme',C('admin_page_theme'));
		//分页跳转的时候保证查询条件
		/*foreach ($map as $key => $val) {
			if (!is_array ($val)) {
				$p->parameter .= "$key=" . urlencode ( $val ) . "&";
			}
		}*/
		if (!empty($map)) {
			$p->parameter .= "key=" . base64_url_encode(json_encode($map)) . "&";
		}
		//dump($list);
		$page = $p->show ();

		$sort = $sort.'_'.$order;
		
		$this->assign('action_link', array('href'=>__URL__.'/addPage/', 'text' => L('addPage')));
		$this->assign('ur_here',L('listPage'));
		$this->assign('nick_name', $nick_name);
		$this->assign('pagestyle',C('admin_page_style'));
		$this->assign('page', $page);
		$this->assign('list',$list);
		$this->assign('sort',$sort);
		$this->display();
	}
	
	//扔进回收站
	public function trashPage() {
		$map	=	array();
		$map['posts_id']	 =	$_REQUEST['id'];
		$page = M('Posts');
		if ($list = $page->where($map)->setField('posts_status', 0)) {
			clear_cache_files();    // 清除缓存
			set_log('trash(Page, Id: '.$_REQUEST['id'].')');
			$this->assign('jumpUrl',__URL__.'/listPage/');
			$this->success(L('success'));
		} else {
			$this->error(L('error'));
		}
	}
	
	public function editPage() {
		$page =  D('Posts');
		$id   =  $_REQUEST['id'];
		$vo	  =  $page->getByPostsId($id);
		$user =  D('AdminUser');
		$nick_name   =  $user->getUserNickName(true,$vo['posts_author']);

		$this->assign('action_link', array('href'=>__URL__.'/listPage/', 'text' => L('listPage')));
		$this->assign('ur_here',L('editPage'));
		$this->assign('nick_name', $nick_name);
		$this->assign('vo',$vo);
		$this->display();
	}	
	
	public function updatePage() {
		$page = D('Posts');
		$data = $page->create();
		if (false === $data) {
			$this->error($page->getError());
		} else {
			if ($page->save($data)) {
				clear_cache_files();    // 清除缓存
				set_log('edit(Page, Id: '.$data['posts_id'].')');
				$this->assign('jumpUrl',__URL__.'/listPage/');
				$this->success(L('success'));
			} else {
				$this->error(L('error'));
			}
		}
	}	
	
	//批量操作
	public function batchPage() {
	}
	
}

?>